package com.raidroid.rdmute;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;

public class Preferences extends PreferenceActivity {
		@Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.preferences);
                PreferenceManager.setDefaultValues(Preferences.this, R.xml.preferences, false);
                
                PreferenceScreen screen;
                screen = getPreferenceScreen();
                
                //Checking Service Status for Preference Screen
                CheckBoxPreference cp = (CheckBoxPreference) screen.findPreference("RDMUte_Service_OnOf");
                if(RDMuteService.IsServiceRunning){
                	cp.setChecked(true);
                }else{
                	cp.setChecked(false);
                }
                
                //RD Mute Service On Of
                cp.setOnPreferenceClickListener(new OnPreferenceClickListener(){
                	public boolean onPreferenceClick(final Preference preference) {
                		CheckBoxPreference cbp = (CheckBoxPreference) preference;
                		if (cbp.isChecked())
                		{
                			startService(new Intent(Preferences.this, RDMuteService.class));
        					if(RDMuteService.IsTaskBarNotificationsOn){
        						createNotification("RD Mute", "RD Mute Running", "RD Mute service started", R.drawable.sound);
        					}
        					Toast.makeText(Preferences.this, "RD Mute Started", 5000).show();
                		}else{
                			stopService(new Intent(Preferences.this, RDMuteService.class));
        					if(RDMuteService.IsTaskBarNotificationsOn){
        						createNotification("RD Mute", "RD Mute Stopped", "RD Mute service stopped", R.drawable.sound);
        					}
        					Toast.makeText(Preferences.this, "RD Mute Stopped", 5000).show();
                		}
                		return true;                		
                	}                	
                });
                // RD Mute Haptic Feedback On Of
                screen.findPreference("RdMute_HapticFeedback_OnOf").setOnPreferenceClickListener(
                		new OnPreferenceClickListener(){
                	public boolean onPreferenceClick(final Preference preference) {
                		CheckBoxPreference cbp = (CheckBoxPreference) preference;
                		if (cbp.isChecked())
                		{
                			RDMuteService.IsHapticFeedbackOn = true;
                		}else{
                			RDMuteService.IsHapticFeedbackOn = false;
                		}
                		return true;                		
                	}                	
                });
                // RD Mute Audio Feedback On Of
                screen.findPreference("RDMute_AudioFeedback_OnOf").setOnPreferenceClickListener(
                		new OnPreferenceClickListener(){
                	public boolean onPreferenceClick(final Preference preference) {
                		CheckBoxPreference cbp = (CheckBoxPreference) preference;
                		if (cbp.isChecked())
                		{
                			RDMuteService.IsSoundFeedbackOn = true;
                		}else{
                			RDMuteService.IsSoundFeedbackOn = false;
                		}
                		return true;                		
                	}                	
                });
                // RD Mute Notifications On Of
                screen.findPreference("RDMute_Notifications_OnOf").setOnPreferenceClickListener(
                		new OnPreferenceClickListener(){
                	public boolean onPreferenceClick(final Preference preference) {
                		CheckBoxPreference cbp = (CheckBoxPreference) preference;
                		if (cbp.isChecked())
                		{
                			RDMuteService.IsTaskBarNotificationsOn = true;
                		}else{
                			RDMuteService.IsTaskBarNotificationsOn = false;
                		}
                		return true;                		
                	}                	
                });                
                //RD Mute Sensivity
                SeekBarPreference skbp = (SeekBarPreference) screen.findPreference("RDMute_Sensivity");
                skbp.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {               	 
                	public boolean onPreferenceChange(Preference preference, Object newValue) {	
                		SeekBarPreference sp = (SeekBarPreference) preference;
						RDMuteService.setSensivity(sp.curprogress);
        				RDMuteService.SEEKBAR_PROGRESS = sp.curprogress;
						return true;
					}
				});
        }
		
	    private void createNotification(String _tickerText, String _contentTitle, String _contentText, int notificationIcon){
	    	
	    	String ns = Context.NOTIFICATION_SERVICE;
	    	NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
	    	
	    	int icon = notificationIcon;
	        CharSequence tickerText = _tickerText;      
	        long when = System.currentTimeMillis(); 
	        Context context = getApplicationContext();
	        CharSequence contentTitle = _contentTitle;
	        CharSequence contentText = _contentText;    

	        Intent notificationIntent = new Intent(this, RDMute.class);
	        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

	        Notification notification = new Notification(icon, tickerText, when);
	        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
	        
	        final int RDMUTE_NOTIFY_ID = 1;
	        mNotificationManager.notify(RDMUTE_NOTIFY_ID, notification);
	    }
}

